package com.example.dto;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ClientAuthenticationMethodDeserializer extends JsonDeserializer<Set<ClientAuthenticationMethod>> {

    @Override
    public Set<ClientAuthenticationMethod> deserialize(JsonParser p, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {
        // 读取 JSON 数组并转换为 List<String>
        List<String> methodNames = p.readValueAs(new com.fasterxml.jackson.core.type.TypeReference<List<String>>() {});

        // 转换为 Set<ClientAuthenticationMethod>
        Set<ClientAuthenticationMethod> methods = new HashSet<>();
        for (String methodName : methodNames) {
            methods.add(new ClientAuthenticationMethod(methodName));
        }

        return methods;
    }
}